package view;

import Bean.Checkout;
import control.HirePayControl;
import java.awt.HeadlessException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class CheckOutPanel extends RootPanel
{

    // Date date;
    int index;
    int choice1;
    int nameroom;
    int tong;
    HirePayControl hpControl = new HirePayControl();
    Checkout Checkout = new Checkout();

    public CheckOutPanel(JFrame window)
    {
        initComponents();
        this.window = window;
        jTDSThuephong.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent me)
            {
                index = jTDSThuephong.getSelectedRow();
                choice1 = (int) jTDSThuephong.getValueAt(index, 0);
                nameroom = (int) jTDSThuephong.getValueAt(index, 8);
            }
        });
        jCBTenKH.setSelected(true);
        jCBTenphong.setSelected(false);
        setTime();
        setBgSearch(50, 18);
        setBackground("pay_panel");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jBTraphong = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTDSThuephong = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        jTFTimkiem = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jCBTenKH = new javax.swing.JCheckBox();
        jCBTenphong = new javax.swing.JCheckBox();
        jDCNgaytra = new com.toedter.calendar.JDateChooser();

        jBTraphong.setText("Trả phòng");
        jBTraphong.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBTraphongActionPerformed(evt);
            }
        });

        jTDSThuephong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]
            {

            },
            new String []
            {

            }
        ));
        jScrollPane1.setViewportView(jTDSThuephong);

        jLabel7.setText("Tìm kiếm theo: ");

        jButton1.setText("Tìm");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Ngày trả");

        jCBTenKH.setText("Tên Khách Hàng");
        jCBTenKH.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBTenKHActionPerformed(evt);
            }
        });

        jCBTenphong.setText("Tên Phòng");
        jCBTenphong.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBTenphongActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1)
                .addGap(30, 30, 30))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(311, 311, 311)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jDCNgaytra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jBTraphong, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(jCBTenKH)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCBTenphong)
                        .addGap(18, 18, 18)
                        .addComponent(jTFTimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap(285, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(jCBTenKH)
                        .addComponent(jCBTenphong))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTFTimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton1)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jDCNgaytra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBTraphong, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(287, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBTraphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBTraphongActionPerformed
        date = jDCNgaytra.getDate();
        if (choice1 == 0)
        {
            JOptionPane.showMessageDialog(null, "Chưa chọn khách hàng");
        }
        else
        {
            if (date == null)
            {
                JOptionPane.showMessageDialog(null, "Chưa chọn ngày trả");
            }
            else
            {
                try
                {
                    Checkout = hpControl.getInforCustomer(choice1);
                    if (date.before(Checkout.getDatecoming()))
                    {
                        JOptionPane.showMessageDialog(null, "Trọn ngày trả lớn hơn ngày thuê");
                    }
                    else
                    {
                        if (JOptionPane.showConfirmDialog(this, "Bạn chắc chắn muốn trả phòng?",
                                "Trả phòng", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                        {
                            Checkout = hpControl.getInforCustomer(nameroom);
                            Checkout.setDateleaving(date);
                            Date Datecoming = Checkout.getDatecoming();
                            Date Dateleaving = Checkout.getDateleaving();
                            long ONE_HOUR = 60 * 60 * 1000L;
                            long noDay = (Dateleaving.getTime() - Datecoming.getTime() + ONE_HOUR) / (ONE_HOUR * 24);
                            if (noDay == 0)
                            {
                                tong = (int) (Integer.parseInt(Checkout.getCostroom()) * 1);
                            }
                            else
                            {
                                tong = (int) (Integer.parseInt(Checkout.getCostroom()) * noDay);
                            }
                            Checkout.setSumcost(Integer.toString(tong));
                            hpControl.addCustomerHistory(Checkout);



                            // new CustomerInvoiceDialog
                            CusromerInvoiceDialog ciDialog = new CusromerInvoiceDialog(Checkout);
                            ciDialog.setVisible(true);
                            window.setEnabled(false);
                            ciDialog.addWindowListener(onClose);

                            // reset room and Customer
                            hpControl.changeRoom(nameroom, "Còn trống");
                            hpControl.deleteCustomer(nameroom);
                            reset();
                        }

                    }
                }
                catch (HeadlessException | NumberFormatException | ParseException ex)
                {
                    Logger.getLogger(CheckOutPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_jBTraphongActionPerformed

    private void jCBTenKHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBTenKHActionPerformed
        // TODO add your handling code here:
        jCBTenphong.setSelected(false);
    }//GEN-LAST:event_jCBTenKHActionPerformed

    private void jCBTenphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBTenphongActionPerformed
        // TODO add your handling code here:
        jCBTenKH.setSelected(false);

    }//GEN-LAST:event_jCBTenphongActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        if (jCBTenKH.getSelectedObjects() != null)
        {
            String key = jTFTimkiem.getText();
            jTDSThuephong.setModel(hpControl.filterCustomer(key));
        }
        else
        {
            String key = jTFTimkiem.getText();
            jTDSThuephong.setModel(hpControl.filterRoom(key));
        }

    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBTraphong;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCBTenKH;
    private javax.swing.JCheckBox jCBTenphong;
    private com.toedter.calendar.JDateChooser jDCNgaytra;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTDSThuephong;
    private javax.swing.JTextField jTFTimkiem;
    // End of variables declaration//GEN-END:variables

    public void reset()
    {
        HirePayControl hipControl = new HirePayControl();
        jTDSThuephong.setModel(hipControl.getAllCustomerhireRoom());
        jTDSThuephong.getTableHeader().setReorderingAllowed(false);
        jDCNgaytra.setDate(null);
    }

    @Override
    public void Show()
    {
        super.Show();
        reset();
    }
}
